﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using MentalAlchemy.Atomics;
using MentalAlchemy.Molecules;

namespace MentalAlchemy.Compounds
{
	public partial class DataTableControl : UserControl
	{
		private string[] header;

		#region - Public properties. -
		/// <summary>
		/// Reads/Writes data as list of string arrays shown in data grid.
		/// </summary>
		public List<string[]> DataRows
		{
			get
			{
				var res = new List<string[]>();
				foreach (DataGridViewRow row in dataGrid.Rows)
				{
					if (row.IsNewRow) continue;
					var tempArray = new string[row.Cells.Count];
					for (int i = 0; i < row.Cells.Count; i++)
					{
						tempArray[i] = (string)row.Cells[i].Value;
					}
					res.Add(tempArray);
				}
				return res;
			}
			set
			{
				if (value == null) return;

				dataGrid.Rows.Clear();
				dataGrid.Columns.Clear();
				foreach (var line in value)
				{
					AddRow(line);
				}
			}
		} 

		/// <summary>
		/// Defines whether 'Load...' button is visible.
		/// </summary>
		public bool ShowLoadButton
		{
			get { return LoadBtn.Visible; }
			set { LoadBtn.Visible = value; }
		}

		/// <summary>
		/// Headers for the columns.
		/// Should be set *after* data is assigned!
		/// </summary>
		public string[] Header
		{
			set
			{
				if (value == null) return;

				header = value;
				for (int i = 0; i < header.Length && i < dataGrid.ColumnCount; i++)
				{
					dataGrid.Columns[i].HeaderText = header[i];
				}
			}
			get
			{
				return header;
			}
		}
		#endregion

		#region - Constructor & Initialization. -
		public DataTableControl()
		{
			InitializeComponent();
		} 

		private void CreateColumns (int colsCount)
		{
			for (int i = 0; i < colsCount; i++)
			{
				string name = "x" + i;
				dataGrid.Columns.Add(name, name);
			}
		}
		#endregion

		#region - Event handlers. -
		private void LoadBtn_Click(object sender, EventArgs e)
		{
			var res = openFileDialog1.ShowDialog();

			if (res == DialogResult.OK)
			{
				string filename = openFileDialog1.FileName;
				var cols = FileIO.ReadColumns(filename, null);
				DataRows = cols;
			}
		}

		private void SaveBtn_Click(object sender, EventArgs e)
		{
			var res = saveFileDialog1.ShowDialog();
			if (res == DialogResult.OK)
			{
				var filename = saveFileDialog1.FileName;

				if (Header != null)
				{
					FileIO.WriteColumns(filename, DataRows, VectorMath.ConvertToString(Header, '\t'));
				}
				else
				{
					FileIO.WriteColumns(filename, DataRows, null);
				}
			}
		}
		#endregion

		#region - Public method. -
		public void AddRow(string[] row)
		{
			if (dataGrid.Columns.Count == 0)
			{	// create columns.
				CreateColumns(row.Length);
			}

			dataGrid.Rows.Add(row);
			
			var idx = dataGrid.Rows.Count - 1;
			dataGrid.Rows[idx].HeaderCell.Value = (idx + 1).ToString();
		} 
		#endregion
	}
}
